<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .menu {
            width: 200px;
            font-size: 13px;
            line-height: 32px;
            text-indent: 10px;
            border: 1px solid #dadce0;
            position: fixed;
            left: 0;
            top: 0;
            z-index: 9999;
            display: none;
            background-color: #fff;
        }

        .menu .nav {
            padding: 5px 0;
        }

        .menu .nav li+li {
            border-top: 1px solid #dadce0;
        }

        .menu .nav li:hover {
            background-color: #e8e8e9;
        }
    </style>
</head>

<body>
    <div class="menu">
        <ul class="nav">
            <li class="copy">复制</li>
            <li class="paste">粘贴</li>
            <li class="cut">剪切</li>
            <li class="refresh">刷新</li>
        </ul>
    </div>
</body>
<script>
    // 自定义菜单
    // 自己定义一个菜单 -> 替换原有的

    var menu = document.getElementsByClassName("menu")[0];

    // 1. 禁用鼠标右键菜单 (鼠标事件)
    document.addEventListener("contextmenu", function (e) {
        var e = e || window.event;
        console.log(e);

        // a. 先禁用原本的
        if (e.preventDefault) {
            e.preventDefault()
        } else {
            e.returnValue = false;
        }

        // b. 在显示自己的
        var x = e.clientX;
        var y = e.clientY;

        menu.style.left = x + "px";
        menu.style.top = y + "px";
        menu.style.display = "block";

    })

    document.addEventListener("mousedown", function () {
        menu.style.display = "none";
    })


    menu.addEventListener("mousedown", function (e) {
        var e = e || window.event;
        if (e.stopPropagation) {
            e.stopPropagation()
        } else {
            e.cancelable = true;
        }
    })

    menu.addEventListener("click", function (e) {
        var e = e || window.event;
        var target = e.target;

        if (target.className == "copy") {
            console.log("复制");
            document.execCommand('copy');  // 老方法 -> 已废弃
        } else if (target.className == "paste") {
            console.log("粘贴");
            document.execCommand('paste');  // 老方法 -> 已废弃
        } else if (target.className == "cut") {
            console.log("剪切");
            document.execCommand('cut');  // 老方法 -> 已废弃
        } else if (target.className == "refresh") {
            console.log("刷新");
            location.reload();
        }
        menu.style.display = "none";
    })

</script>

</html>